<template>
	<view>
		<scan-layout ref="scanLayout" :tabsList="tabsList" @add-click="addClick" @product-change="productChange" @confirm="confirmClick" sum-confirm-text="销售出货">
			<u-form :model="form" ref="form" label-width="180" slot="ext-form">
				<u-form-item label="单价" prop="salePrice"><u-input v-model="form.salePrice" type="number" @blur="prizeBlur"></u-input></u-form-item>
				<u-form-item label="销售人员" prop="salor"><u-input v-model="form.salor" @blur="salorBlur" /></u-form-item>
			</u-form>
		</scan-layout>
	</view>
</template>

<script>
import { mapActions, mapMutations } from 'vuex';
import ScanLayout from './components/scanLayout/scanLayout.vue';
export default {
	components: {
		ScanLayout
	},
	data() {
		return {
			tabsList: [
				{
					name: '扫码'
				},
				{
					name: '数据汇总'
				}
			],
			rules: {},
			form: {
				salePrice: '',
				salor: '导购'
			}
		};
	},
	onReady() {
		this.$refs.form.setRules(this.rules);
	},
	beforeDestroy() {
		this.resetScanDeviceInfo();
		this.resetSumList();
	},
	methods: {
		...mapActions({
			saleDevice: 'storageOrder/saleDevice',
			getDeviceByCustomer: 'storageOrder/getDeviceByCustomer'
		}),
		...mapMutations({
			resetSumList: 'storageOrder/resetSumList',
			dropSumListItem: 'storageOrder/dropSumListItem',
			setSumList: 'storageOrder/setSumList',
			resetScanDeviceInfo: 'storageOrder/resetScanDeviceInfo',
			updateScanDeviceInfo: 'storageOrder/updateScanDeviceInfo',
			updateCanAdd: 'storageOrder/updateCanAdd'
		}),
		outStoragePickerConfirm(e) {
			console.log(e);
			this.form.outStorageType = e[0].value;
			this.showForm.outStorageType = e[0].label;
			if (e[0].value == '0') {
				this.form.loadShop = '';
			}
		},
		productChange(e) {
			if (e.isValid) {
				this.getDeviceByCustomer({
					code: this.$store.state.storageOrder.scanDeviceInfo.code
				})
					.then(res => {
						console.log(res);
						if (res && res.code == 0 && res.data) {
							if (res.data.ckZt == 1 || res.data.xsZt == 1) {
								this.updateCanAdd(false);
								uni.showToast({
									title: '该机身码已出货',
									icon: 'none'
								});
								return;
							}
							if (res.data.rkZt) {
								this.$refs.form.validate(valid => {
									if (valid) {
										console.log('pageForm', valid);
										this.updateCanAdd(true);
									} else {
										this.updateCanAdd(false);
									}
								});
							} else {
								this.updateCanAdd(false);
								uni.showToast({
									title: '机身码尚未入库',
									icon: 'none'
								});
							}
						} else {
							this.updateCanAdd(false);
							uni.showToast({
								title: '机身码尚未入库',
								icon: 'none'
							});
						}
					})
					.catch(err => {
						console.log(err);
						this.updateCanAdd(false);
						uni.showToast({
							title: '网络错误，查询机身码失败',
							icon: 'none'
						});
					});
			} else {
				this.updateCanAdd(false);
			}
		},
		prizeBlur(e){
			this.updateScanDeviceInfo({
				salePrice:this.form.salePrice
			})
		},
		salorBlur(e){
			this.updateScanDeviceInfo({
				salor:this.form.salor
			})
		},
		addClick() {
			this.$refs.form.validate(valid => {
				if (valid) {
					this.$refs.scanLayout.addToList();
					// this.form = { salePrice: '', salor: '导购' };
					this.updateScanDeviceInfo(this.form)
				}
			});
		},
		confirmClick() {
			this.$confirm('提示', '确定出货？', this.outStorage);
		},
		async outStorage() {
			uni.showLoading({
				mask: true,
				title: '请求中'
			});
			let list = this.$store.state.storageOrder.sumList;
			let successCodeArr = [];
			let failCodeArr = [];
			for (let i = 0; i < list.length; i++) {
				let data = list[i];
				if (data.checked) {
					try {
						let res = await this.saleDevice({
							...data
						});
						if (res && res.code == 0) {
							successCodeArr.push(data.code);
						} else {
							failCodeArr.push(data.code);
						}
					} catch (e) {
						failCodeArr.push(data.code);
						continue;
					}
				}
			}
			uni.hideLoading();
			if (failCodeArr.length > 0) {
				let newList = [];
				list.forEach(e => {
					if (successCodeArr.indexOf(e.code) == -1) {
						newList.push(e);
					}
				});
				this.setSumList(newList);
				uni.showModal({
					title: `有${failCodeArr.length}条数据请求失败，请重新尝试`,
					showCancel: false
				});
			} else {
				this.dropSumListItem();
				uni.showToast({
					icon: 'success',
					title: '提交成功'
				});
			}
		}
	}
};
</script>

<style lang="less">
@import './styles/table.less';
</style>
